home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / NOCR.ICN < prev    next >
Text File  |  1992-09-28  |  4KB  |  132 lines

  1. ############################################################################
  2. #
  3. #    File:     nocr.icn
  4. #
  5. #    Subject:  Program to convert MS-DOS text files to UNIX
  6. #
  7. #    Author:   Richard L. Goerwitz
  8. #
  9. #    Date:     December 30, 1991
  10. #
  11. ###########################################################################
  12. #
  13. #    Version:  1.4
  14. #
  15. ###########################################################################
  16. #  
  17. #    This program simply converts \r\n to \n in each line of each of the
  18. #  files supplied as command-line arguments, thereby effecting conversion
  19. #  of MS-DOS format text files to the corresponding UNIX format.
  20. #
  21. #    usage:  nocr file1 [file2 [etc.]]
  22. #
  23. #    No check done to see whether the file is in fact a text file.
  24. #
  25. ############################################################################
  26. #
  27. #  Requires:  UNIX or MS-DOS
  28. #
  29. #  See also: yescr.icn
  30. #
  31. ############################################################################
  32.  
  33. procedure main(a)
  34.  
  35.     local fname, infile, outfile, line, temp_name
  36.  
  37.     # Static variables, initial clause not really necessary in main().
  38.     static slash, l, ms, DOSos, nok, ok
  39.     initial {
  40.  
  41.     nok := string(~&letters)
  42.     ok := repl("X",*nok)
  43.  
  44.     # Find us a place to put temporary files.
  45.     if find("UNIX",&features) then {
  46.         slash := "/"
  47.         l := 10
  48.         ms := ""
  49.     }
  50.     else if find("MS-DOS", &features) then {
  51.         slash := "\\"
  52.         l := 8
  53.         ms := "u"
  54.         DOSos := 1
  55.     }
  56.     # Don't take this out unless you're sure of what you're doing.
  57.     else stop("nocr:  tested only under UNIX and MS-DOS")
  58.     }
  59.  
  60.     # Check to see if we have any arguments.
  61.     *a = 0 & stop("usage:  nocr file1 [file2...]")
  62.  
  63.     # Start popping filenames off of the argument list.
  64.     while fname := pop(a) do {
  65.  
  66.     # Open input file.
  67.     infile := open(fname,"r") | (er_out(fname), next)
  68.     # Get temporary file name.
  69.     every temp_name :=
  70.         pathname(fname, slash) ||
  71.         map(left(basename(fname,slash),l,"X"), nok, ok) ||
  72.         "." || right(0 to 999,3,"0")
  73.     do close(open(temp_name)) | break
  74.     # Open temporary file.
  75.     outfile := open(\temp_name,"w"||ms) | (er_out(fname), next)
  76.  
  77.     if \DOSos then {
  78.         # Infile above was opened in translate mode (removing the CR),
  79.         # while outfile was opened in untranslate mode (automatically
  80.             # writing the line in UNIX format).
  81.         while write(outfile,read(infile))
  82.     }
  83.     else {
  84.         # If not running under DOS, then we're under UNIX (unless
  85.         # we've been hacked).  Trim CR manually, then write.
  86.         while line := read(infile) do {
  87.                 if line[-1] == "\x0D" then
  88.             line[-1] := ""
  89.             write(outfile, line)
  90.             }
  91.     }
  92.  
  93.     # Close opened input and output files.
  94.     close(infile)  | stop("nocr:  cannot close, ",fname,"; aborting")
  95.     close(outfile) | stop("nocr:  cannot close, ",temp_name,"; aborting")
  96.  
  97.     # Remove physical input file.
  98.     remove(fname) | stop("nocr:  cannot remove ",fname,"; aborting")
  99.  
  100.     # Give temp name the same name as the input file, completing the
  101.     # conversion process.
  102.     rename(temp_name,fname) |
  103.         stop("nocr:  Can't find temp file ",temp_name,"; aborting")
  104.     }
  105.  
  106. end
  107.  
  108.  
  109. procedure er_out(s)
  110.     write(&errout,"nocr:  cannot open ",s," for reading")
  111.     return
  112. end
  113.  
  114.  
  115. procedure basename(s,slash)
  116.     s ? {
  117.     while tab(find(slash)+1)
  118.     return tab(0)
  119.     }
  120. end
  121.  
  122.  
  123. procedure pathname(s,slash)
  124.     local s2
  125.  
  126.     s2 := ""
  127.     s ? {
  128.     while s2 ||:= tab(find(slash)+1)
  129.     return s2
  130.     }
  131. end
  132.